<?php

namespace App\Admin\Controller\Base;


use App\AbstractController;
use App\Admin\Service\Base\SuperConfigService;
use App\Common\Exception\ApiMessageException;
use App\Common\Model\Base\SuperConfig;
use App\Common\Util\EasySearch;
use App\Common\Util\Response;
use App\Common\Util\TP;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use think\annotation\route\Get;
use think\annotation\route\Group;
use think\annotation\route\Post;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

/**
 * 超级配置控制器
 */
class SuperConfigController extends AbstractController
{


    /**
     * 列表页
     *
     * @return string
     */
    public function lists(): string
    {
        return Response::render('Base/SuperConfig/lists');
    }

    /**
     * 列表数据
     *
     * @return \think\Response
     */
    public function listsData(): \think\Response
    {
        return Response::json()->success(SuperConfigService::aop()->lists());
    }


    /**
     * 创建数据页面
     *
     * @return string
     */
    public function create(): string
    {
        return Response::render('Base/SuperConfig/edit');
    }


    /**
     * 创建数据
     *
     * @return \think\Response
     * @throws ApiMessageException
     */
    public function store(): \think\Response
    {
        SuperConfigService::aop()->store(TP::request()->post());

        return Response::json()->success();
    }

    /**
     * 更新数据页面
     *
     * @return string
     */
    public function edit(): string
    {
        return Response::render('Base/SuperConfig/edit');
    }

    /**
     * 更新数据
     *
     * @return \think\Response
     * @throws ApiMessageException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function update(): \think\Response
    {
        SuperConfigService::aop()->update(TP::request()->post());

        return Response::json()->success();
    }

    /**
     * 删除数据
     *
     * @return \think\Response
     * @throws ApiMessageException
     */
    public function destroy(): \think\Response
    {
        SuperConfigService::aop()->destroy(TP::request()->post('ids'));

        return Response::json()->success();
    }

    /**
     * 基础配置
     *
     * @return string
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function baseConfig(): string
    {
        $superConfig = SuperConfig::find(TP::request()->get('id'));

        return Response::render($superConfig->type < 4
            ? 'Base/SuperConfig/config'
            : 'Base/SuperConfig/advancedArrayConfig',

            ['superConfig' => $superConfig]
        );
    }

    /**
     * 保存|更新基础配置
     *
     * @return \think\Response
     * @throws ApiMessageException
     */
    public function updateBaseConfig(): \think\Response
    {
        SuperConfigService::aop()->saveBase(TP::request()->post());

        return Response::json()->success();
    }

    /**
     * 高级数组数据
     *
     * @return \think\Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function advancedArrayData(): \think\Response
    {
        $data = SuperConfigService::aop()->advancedArrayData(
            (int)TP::request()->get('page') ?: 1,
            (int)TP::request()->get('limit') ?: 10,
        );

        return Response::json()->success($data);
    }

    /**
     * 高级数组数据删除
     *
     * @return \think\Response
     * @throws ApiMessageException
     */
    public function advancedArrayDataDestroy(): \think\Response
    {
        $id        = TP::request()->post('id');
        $deleteIds = TP::request()->post('ids');

        SuperConfigService::aop()->advancedArrayDataDestroy((int)$id, $deleteIds ?? []);

        return Response::json()->success();
    }
}
